package datasource;

import datasource.config.C3P0Config;
import homework.jdbc.domain.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * Created by Administrator on 2017/9/14.
 */
public class UserDao {
    public int pageSize = 3;

    public List<User> findPage(int page) throws SQLException {
        //获得数据库连接
        Connection conn = C3P0Config.getConnection();
        List<User> list = new ArrayList<>();
        //编写sql语句
        String sql = "select * from user order by id asc limit ?,?";
        //预编译处理
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1,(page-1)*pageSize);
        ps.setInt(2,pageSize);
        //执行语句
        ResultSet result = ps.executeQuery();
        while (result.next()){
            User user = new User();
            user.setId(result.getInt(1));
            user.setUsername(result.getString(2));
            user.setPassword(result.getString(3));
            user.setEmail(result.getString(4));
            user.setPhone(result.getString(5));
            list.add(user);
        }
        return  list;
        //处理结果
    }
}
